package com.hnas.api.server;

import java.net.URLEncoder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hnas.api.domain.DataType;
import com.hnas.api.domain.RouteFinalValue;
import com.hnas.api.dto.CommonKeyData;
import com.hnas.api.dto.DTOApiResponse;
import com.hnas.api.dto.DTORequest;
import com.hnas.api.dto.DTOResult;
import com.hnas.api.dto.PostKey;
import com.hnas.api.util.AESTool;
import com.hnas.api.util.HttpRequest;
import com.hnas.api.util.SignatureUtil;
import com.hnas.core.util.StringUtil;

@Component("initApp")
public class InitApp {

	private static Log log = LogFactory.getLog(InitApp.class);

	private static ObjectMapper objectMapper = new ObjectMapper();

	@Autowired
	private CommonKeyData ckd;

	AESTool aes = new AESTool();

	SignatureUtil signatureUtil = new SignatureUtil();

	/**
	 * 初始化获取密钥和令牌
	 */
	public PostKey initApp(String url) throws Exception {
		PostKey postKey = new PostKey();
		try {
			log.info("开始初始化initApp");
			// 加密类
			String appid = ckd.getAppid();// 公司标识
			String token = ckd.getToken();// 令牌
			String route = RouteFinalValue.APP_Init;// 路由名称
			String key = ckd.getKey();// 加密key
			String termNo = ckd.getTermNo();// 设备编号，唯一

			// 请求参数
			DTORequest dtoRequest = new DTORequest();
			dtoRequest.setAppid(appid);// APP标识
			long millis = System.currentTimeMillis();
			dtoRequest.setMillis(millis);// 时间戳
			String version = "1.0.0";
			dtoRequest.setVersion(version);// 接口版本
			dtoRequest.setRoute(route);// 路由名称
			dtoRequest.setToken(token);// 令牌
			dtoRequest.setTermNo(termNo);// 设备编号

			// 数据签名
			String lol = "";// 摘要
			String sign = signatureUtil.generateSignature(appid, token, lol, millis);// 签名
			dtoRequest.setSign(sign);
			// 请求实体转json
			String requestJson = objectMapper.writeValueAsString(dtoRequest);// 实体转json
			// pos数据
			// 发送 POST 请求
			StringBuffer param = new StringBuffer();
			param.append("dataType=" + DataType.JSON);
			if (StringUtil.isNotEmpty(requestJson)) {
				requestJson = URLEncoder.encode(requestJson, "UTF-8");
			}
			param.append("&sendData=" + requestJson);
			// 发送数据
			String requestData = HttpRequest.sendPost(url, param.toString());

			// json转为实体。
			DTOApiResponse dtoResponse = new DTOApiResponse();
			String resultData = "";
			if (!requestData.equals("")) {
				dtoResponse = objectMapper.readValue(requestData, DTOApiResponse.class);
				DTOResult dtoResult = dtoResponse.getResult();
				resultData = dtoResult.getResultData();
			}
			if (StringUtil.isNotEmpty(resultData)) {
				resultData = aes.decrypt(resultData, key);// 解密
				postKey = objectMapper.readValue(resultData, PostKey.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return postKey;
	}
}
